import 'package:flutter/material.dart';
import 'package:tetris/view/MyMaterial.dart';


const _DIGITAL_ROW_SIZE = Size(14, 24);

class Number extends StatelessWidget {
  final int length;

  ///the number to show
  ///could be null
  final int number;

  final bool padWithZero;

  Number(
      {Key key,
      this.length = 5,
      @required this.number,
      this.padWithZero = false})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    String digitalStr = number?.toString() ?? "";
    if (digitalStr.length > length) {
      digitalStr = digitalStr.substring(digitalStr.length - length);
    }
    digitalStr = digitalStr.padLeft(length, padWithZero ? "0" : " ");
    List<Widget> children = [];
    for (int i = 0; i < length; i++) {
      children.add(Digital(int.tryParse(digitalStr[i])));
    }
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: children,
    );
  }
}




class IconPause extends StatelessWidget {
  final bool enable;
  final Size size;

  const IconPause({Key key, this.enable = true, this.size = const Size(18, 16)})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MyMaterial(
      widgetSize: size,
      srcSize: const Size(20, 18),
      srcOffset: enable ? const Offset(75, 75) : const Offset(100, 75),
    );
  }
}

class IconSound extends StatelessWidget {
  final bool enable;
  final Size size;

  const IconSound({Key key, this.enable = true, this.size = const Size(18, 16)})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MyMaterial(
      widgetSize: size,
      srcSize: const Size(25, 21),
      srcOffset: enable ? const Offset(150, 75) : const Offset(175, 75),
    );
  }
}

class IconColon extends StatelessWidget {
  final bool enable;

  final Size size;

  const IconColon({Key key, this.enable = true, this.size = const Size(10, 17)})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MyMaterial(
      widgetSize: size,
      srcOffset: enable ? const Offset(229, 25) : const Offset(243, 25),
      srcSize: _DIGITAL_ROW_SIZE,
    );
  }
}

/// a single digital
class Digital extends StatelessWidget {
  ///number 0 - 9
  ///or null indicate it is invalid
  final int digital;

  final Size size;

  Digital(this.digital, {Key key, this.size = const Size(10, 17)})
      : assert(digital == null || (digital <= 9 && digital >= 0)),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return MyMaterial(
      widgetSize: size,
      srcOffset: _getDigitalOffset(),
      srcSize: _DIGITAL_ROW_SIZE,
    );
  }

  Offset _getDigitalOffset() {
    int offset = digital ?? 10;
    final dx = 75.0 + 14 * offset;
    return Offset(dx, 25);
  }
}

